<template>
    <div class="main_right_nav_2">
        <div class="main_right_nav_2_hot_title">
            热评
        </div>
        <div class="main_right_nav_2_hot_reply">
            <div v-for="item in hotReplyList" :key="item.id">
                <div class="main_right_nav_2_hot_reply_one">
                    <img :src="item.avatar" class="main_right_nav_2_hot_reply_avatar" alt="头像">
                    <div>
                        <div class="main_right_nav_2_hot_reply_nickname"><strong>{{ item.nickname }}</strong></div>
                        <div class="main_right_nav_2_hot_reply_time">{{ item.createTime }}</div>
                    </div>
                </div>
                <div @click="handleGoPage(item.problemId)" class="main_right_nav_2_hot_reply_content"
                     v-dompurify-html="item.content"></div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import {hopReply} from "@/api/home/answer";
import {onMounted, ref} from "vue";
import router from "@/router";

//根据热评id动态跳转页面
const handleGoPage = (id: number) => {
    router.push("/problemDetails?id=" + id)
}

interface HotReply {
    id: number,
    avatar: string,
    nickname: string,
    content: string,
    createTime: Date,
    problemId: number
}

//前五条热评列表
const hotReplyList = ref<HotReply[]>([])

//热评
const handleHotReply = () => {
    hopReply().then((res) => {
        hotReplyList.value = res.data.data.list
        const regex = new RegExp('<img', 'gi')
        hotReplyList.value.map((item) => {
            item.content = item.content.replace(regex, `<img style="max-width: 50%; height: auto"`)
        })
    })
}

onMounted(() => {
    handleHotReply()
})
</script>

<style lang="less" scoped>
.main_right_nav_2 {
  margin-top: 3%;
  background-color: #ffffff;
  margin-left: 10px;

  .main_right_nav_2_hot_title {
    text-align: center;
    font-size: 20px;
    font-weight: 600;
  }

  .main_right_nav_2_hot_reply {

    .main_right_nav_2_hot_reply_one {
      display: flex;

      .main_right_nav_2_hot_reply_avatar {
        border-radius: 50%;
      }

      .main_right_nav_2_hot_reply_nickname {
        margin-top: 3px;
        margin-left: 5px;
      }

      .main_right_nav_2_hot_reply_time {
        margin-left: 5px;
      }
    }

    .main_right_nav_2_hot_reply_content {
      width: 90%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      max-height: 50px;
      height: auto;
      margin-left: 20px;
      cursor: pointer;
      border-bottom: 1px solid #f0f0f0;
    }

    .main_right_nav_2_hot_reply_content:hover {
      background-color: rgba(0, 0, 0, 0.1);
    }
  }
}

img {
  max-width: 100px;
  width: 50px;
}
</style>